$(document).ajaxSend(function(event, xhr, settings) {
	function getCookie(name) {
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
	function sameOrigin(url) {
		// url could be relative or scheme relative or absolute
		var host = document.location.host; // host + port
		var protocol = document.location.protocol;
		var sr_origin = '//' + host;
		var origin = protocol + sr_origin;
		// Allow absolute or scheme relative URLs to same origin
		return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
		(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
		// or any other URL that isn't scheme relative or absolute i.e relative.
		!(/^(\/\/|http:|https:).*/.test(url));
	}
	function safeMethod(method) {
		return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
	}

	if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
		xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
	}
});

function tm(unix_tm) {
	var dt = new Date(unix_tm*1000);
	return (dt.getMonth()+1) + '/' + 
	dt.getDate() + '/' + 
	dt.getFullYear();
}

function dtDataRender(data, type, full) {
	return JSON.stringify(data);
}

function dtBooleanRender(data, type, full) {
	return ('' + data).toLowerCase();
}

function dtTimestampRender(data, type, full) {
	return tm(data)
}

//A class represents a table of testcases
function TestcaseTable (config, mode) {
	this.config = config;
	var aoColumns = config.aoColumns;

	for(var i = 0; i < aoColumns.length; i++){
		if(aoColumns[i].semanticType){
			var argType = aoColumns[i].semanticType;
			if(argType.substring(argType.length-2) == '[]'){
				aoColumns[i]['mRender'] = dtDataRender;
			} else if(argType == 'String') {
				aoColumns[i]['mRender'] = dtDataRender;
			} else if(argType == 'Boolean') {
				aoColumns[i]['mRender'] = dtBooleanRender;
			}
		}

		if(aoColumns[i].meta){
			var meta = aoColumns[i].meta;

			// find out meta info
			if(meta == 'my_sol') this.mySolIdx = i;
			if(meta == 'ref_sol_1') this.refSol1Idx = i;
			if(meta == 'ref_sol_2') this.refSol2Idx = i;
			if(meta == 'answer') this.answerIdx = i;
			if(meta == 'author') this.authorIdx = i;
			if(meta == 'timestamp') {
				this.timestampIdx = i;
				aoColumns[i]['mRender'] = dtTimestampRender;
			}
		}

	}// for

	this.idIdx = 0;
	this.dataTable = $('#testcase_table').dataTable(config);

	this.setMySolVis(false);
	this.setRefSol1Vis(false);
	this.setRefSol2Vis(false);
	this.setIdVis(false);
	this.setTimestampVis(false);
	if(mode == 'for_run'){
		this.setAuthorVis(false);
	}
	if(mode == 'for_edit'){
		this.setAnswerVis(false);
	}
}

TestcaseTable.prototype.updateResult = function(colName, oJson) {
	var idx = this.mySolIdx;
	if(colName == 'ref_sol_1') idx = this.refSol1Idx;
	else if(colName == 'ref_sol_2') idx = this.refSol2Idx;

	var rows = this.dataTable.fnGetNodes();
	for(var i = 0; i < rows.length; i ++) {
		var row = rows[i];
		var t_id = 't_' + this.dataTable.fnGetData(row, this.idIdx);
		var val = oJson[t_id];
		this.dataTable.fnUpdate(val, row, idx);
	}
	this.dataTable.fnSetColumnVis(idx, true);
}

TestcaseTable.prototype.hideRunResult = function() {
	this.setMySolVis(false);
	this.setRefSol1Vis(false);
	this.setRefSol2Vis(false);
}

TestcaseTable.prototype.setIdVis = function(b) {
	this.dataTable.fnSetColumnVis(0, b);
}

TestcaseTable.prototype.setMySolVis = function(b) {
	this.dataTable.fnSetColumnVis(this.mySolIdx, b);
}

TestcaseTable.prototype.setRefSol1Vis = function(b) {
	this.dataTable.fnSetColumnVis(this.refSol1Idx, b);
}

TestcaseTable.prototype.setRefSol2Vis = function(b) {
	this.dataTable.fnSetColumnVis(this.refSol2Idx, b);
}

TestcaseTable.prototype.setAnswerVis = function(b) {
	this.dataTable.fnSetColumnVis(this.answerIdx, b);
}

TestcaseTable.prototype.setAuthorVis = function(b) {
	this.dataTable.fnSetColumnVis(this.authorIdx, b);
}

TestcaseTable.prototype.setTimestampVis = function(b) {
	this.dataTable.fnSetColumnVis(this.timestampIdx, b);
}

TestcaseTable.prototype.addRow = function(row) {
	alert('not implemented')
}

TestcaseTable.prototype.showResult = function(oJson) {
	alert('not implemented')
}

function set_tab (tabId) {
	$(tabId).addClass('current');
}
